package com.demo.action;

import com.demo.model.Blog;
import com.demo.model.News;
import com.demo.validator.BlogValidator;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

/**
 * BlogController
 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
 */
public class NewsController extends Controller {
	public void index() {
		Integer pageNum = getParaToInt("pageNum");
		String type = getPara("type");
		if(null==pageNum)  pageNum=1;//默认第一页
		setAttr("newsPage", News.me.paginate(pageNum, 6 , type));
		render("news_list.jsp");
	}
	
	public void detail() {
		Integer nid = getParaToInt("nid");
		setAttr("news", News.me.findById(nid));
		render("news_content.jsp");
	}
	
	@Before(BlogValidator.class)
	public void save() {
		getModel(Blog.class).save();
		redirect("/news");
	}
	
	public void edit() {
		setAttr("news", Blog.me.findById(getParaToInt()));
	}
	
	@Before(BlogValidator.class)
	public void update() {
		getModel(Blog.class).update();
		redirect("/news");
	}
	
	public void delete() {
		Blog.me.deleteById(getParaToInt());
		redirect("/news");
	}
}


